var status = 0;
var selectedType = -1;
var selectedItem = -1;
var item;
var mats;
var matQty;
var cost;
var qty;
var equip;
var last_use; //last item is a use item
var 正方箭头 = "#fUI/Basic/BtHide3/mouseOver/0#";
var 是否为药王;
function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == 1)
        status++;
    else
        cm.dispose();
    if (status == 0) {
		if(cm.getPlayer().getPrizeLog("1级药王"))
		{
			var selStr = "#k我可以帮你制作一些特殊的药水，前提是你得有材料 \r\n#b你的副职业是药王，将获得双倍制作#k";
			是否为药王 = true;
		}
		else
		{
			var selStr = "我可以帮你制作一些特殊的药水，前提是你得有材料";
		}
        var options = new Array(正方箭头+"#b 材料如何获得？",正方箭头+"#b 制作药水");
        for (var i = 0; i < options.length; i++) {
            selStr += "\r\n#L" + i + "# " + options[i] + "#l";
        }

        cm.sendSimple(selStr);
    } else if (status == 1) {
        if (selection < 0) {
            cm.sendOk("腳本出錯，請聯繫管理員...");
            cm.dispose();
            return;
        }
        selectedType = selection;
        if (selectedType == 1) { //mineral refine
            var selStr = "你想制作哪种药水？#b";
            var minerals = new Array("#z2022244#", "#z2020031#", "#z2020011#", "#z2022010#", "#z2022011#", "#z2002023#", "#z2022015#", "#z2022195#", "#z2023247#", "#z2022229#", "#z2022955#", "#z2022195#", "#z2022956#", "#z2022123#", "#z2022179#", "#z2022141#");
            for (var i = 0; i < minerals.length; i++) {
                selStr += "\r\n#L" + i + "# " + minerals[i] + "#l";
            }
            equip = false;
            cm.sendSimple(selStr);
        }
		else if (selectedType == 0)
		{
			cm.sendOk("#b合成药水材料通过回收各类小怪掉落的材料所获得\r\n#r在拍卖-回收中心-材料回收");
			cm.dispose();
		}
        if (equip)
            status++;
    } else if (status == 2 && mode == 1) {
        if (selection < 0) {
            cm.sendOk("腳本出錯，請聯繫管理員...");
            cm.dispose();
            return;
        }
        selectedItem = selection;
		if (selectedType == 1) 
		{ 
            var itemSet = new Array(2022244, 2020031, 2020011, 2022010, 2022011, 2002023, 2022015, 2022195, 2023247,2022229,2022955,2022195,2022956,2022123,2022179,2022141);
            var matSet = new Array(
			new Array(4000041, 4007002,4007001), //1
			new Array(4000041, 4007001,4007003), //2
			new Array(4000041, 4007004,4007005), //3
			new Array(4000041, 4007001, 4007003),//4
			new Array(4000041, 4007001, 4007003, 4007002),//5
			new Array(4000041, 4007004, 4007007, 4007002),//6
			new Array(4000041, 4007003, 4007004, 4007001),//7
			new Array(4000041, 4007002, 4007004, 4007006),//8
			new Array(4000041, 4007005, 4007002, 4007003),//9
			new Array(4000041, 4007001, 4007007, 4007004),//10
			new Array(4000041, 4007003, 4007006, 4007002),//11
			new Array(4000041, 4007007, 4007001, 4007006),//12
			new Array(4000041, 4007005, 4007003, 4007004),//13
			new Array(4000041, 4007007, 4007006, 4007005),//14
			new Array(4000041, 4007002, 4007004, 4007001),//15
			new Array(4000041, 4007003, 4007005, 4007007)//16
			);
            var matQtySet = new Array(
			new Array(1, 1, 1), //1
			new Array(1, 1, 2), //2
			new Array(1, 2, 1),//3
			new Array(1, 2, 2),//4
			new Array(1, 2, 2),//5
			new Array(1, 2, 1,2),//6
			new Array(1, 2, 1,2),//7
			new Array(1, 2, 2,2),//8
			new Array(2, 2, 1,2),//9
			new Array(2, 1, 1,1),//10
			new Array(2, 5, 2,2),//11
			new Array(3, 1, 1,1),//12
			new Array(3, 4, 3,2),//13
			new Array(3, 2, 3,2),//14
			new Array(10, 4, 5,8),//15
			new Array(10, 2, 5,6)//16
			);
            var costSet = new Array(4500, 4500, 4500, 4500, 4500, 4500, 4500, 9000, 27000, 27000, 36000, 42000, 57000, 65000, 130000, 127000);
            item = itemSet[selectedItem];
            mats = matSet[selectedItem];
            matQty = matQtySet[selectedItem];
            cost = costSet[selectedItem];
        }
		else if (selectedType == 2) { //special refine
            var itemSet = new Array(4005000, 4005001, 4005002, 4005003);
            var matSet = new Array(4004000, 4004001, 4004002, 4004003);
            var matQtySet = new Array(10, 10, 10, 10);
            var costSet = new Array(45000, 45000, 45000, 45000);
            item = itemSet[selectedItem];
            mats = matSet[selectedItem];
            matQty = matQtySet[selectedItem];
            cost = costSet[selectedItem];
        }

        var prompt = "你想做多少个#b#z" + item + "# ？";

        cm.sendGetNumber(prompt, 1, 1, 100)
    } else if (status == 3) {
        if (selection < 0) {
            cm.sendOk("腳本出錯，請聯繫管理員...");
            cm.dispose();
            return;
        }
        if (equip)
        {
            selectedItem = selection;
            qty = 1;
        } else
            qty = selection;

        last_use = false;

        if (selectedType == 3) { //claw refine
            var itemSet = new Array(1472023, 1472024, 1472025);
            var matSet = new Array(new Array(1472022, 4011007, 4021000, 2012000), new Array(1472022, 4011007, 4021005, 2012002), new Array(1472022, 4011007, 4021008, 4000046));
            var matQtySet = new Array(new Array(1, 1, 8, 10), new Array(1, 1, 8, 10), new Array(1, 1, 3, 5));
            var costSet = new Array(80000, 80000, 100000)
            item = itemSet[selectedItem];
            mats = matSet[selectedItem];
            matQty = matQtySet[selectedItem];
            cost = costSet[selectedItem];
            if (selectedItem != 2)
                last_use = true;
        }

        var prompt = "你想要制作 ";
        if (qty == 1)
            prompt += "1个 #t" + item + "#?";
        else
            prompt += qty + " 个 #t" + item + "#?\r\n";

        prompt += "请确保你的背包有足够的空间，丢失概不负责#b\r\n\r\n#d需要材料：";

        if (mats instanceof Array) {
            for (var i = 0; i < mats.length; i++) {
                prompt += "\r\n#i" + mats[i] + "# " + matQty[i] * qty + " #t" + mats[i] + "#";
            }
        } else {
            prompt += "\r\n#i" + mats + "# " + matQty * qty + " #t" + mats + "#";
        }

        if (cost > 0) {
            prompt += "\r\n#i4031138# " + cost * qty + " 金币";
        }
        cm.sendYesNo(prompt);
    } else if (status == 4) {
        var complete = true;

        if (cm.getMeso() < cost * qty) {
            cm.sendOk("你没钱你来找我做什么")
            cm.dispose();
            return;
        } else {
            if (mats instanceof Array) {

                for (var i = 0; complete && i < mats.length; i++)
                {
                    if (!cm.haveItem(mats[i], matQty[i] * qty))
                    {
                        complete = false;
                    }
                }
            } else {
                if (!cm.haveItem(mats, matQty * qty))
                {
                    complete = false;
                }
            }
        }

        if (!complete)
            cm.sendOk("你的材料不够啊，仔细检查一下，我可不做亏本的买卖");
        else {
            if (mats instanceof Array) {
                for (var i = 0; i < mats.length; i++) {
                    cm.gainItem(mats[i], -matQty[i] * qty);
                }
            } else {
                cm.gainItem(mats, -matQty * qty);
            }
            cm.gainMeso(-cost * qty);
            cm.gainItem(item, qty);
			if(是否为药王 == true)
			{
				cm.gainItem(item, qty);
				cm.sendNext("药王，你的东西做好了！");
			}
			else
			{
				cm.sendNext("嗯,做好了，这药剂看起来很猛，希望你能喜欢它");
			}
        }
        cm.dispose();
    }
}